home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / acron24a / amicron24.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  9KB  |  366 lines

  1. /*
  2.  *    amicron.c    (Version 2.4(a&L) by <CB>)
  3.  *
  4.  *    Public Domain (p) No Rights Reserved
  5.  *
  6.  *    This program operates as a daemon, waking up every minute
  7.  *    to execute the CRONTAB table.  
  8.  *
  9.  *    Put in startup-sequence like:
  10.  *        run newcli con:0/140/160/50/CronTask s:startcron
  11.  *    The startcron file needs to contains just one line:
  12.  *        amicron
  13.  *
  14.  *    Some notes are included below concerning the cron table
  15.  *    file format.  In this version the cron table file is left
  16.  *    on disk and not moved to core, and  the command character
  17.  *    to signify a newline '%' is not used.
  18.  *
  19.  *    Some cron table entry examples:
  20.  *
  21.  *    Print the date in the crontask window every minute:
  22.  *        * * * * * date
  23.  *
  24.  *    Print the date in the crontask window on the hour, every hour:
  25.  *        0 * * * * date
  26.  *
  27.  *    Run uupc at 4:30 am every day except Sat and Sun:
  28.  *        30 4 * * 1-5 uupc -siscuva
  29.  *
  30.  *    Backup the files every other day at 7:30 pm:
  31.  *        30 19 * * 1,3,5 sdbackup -s LAST dh0: incbkup_1:
  32.  *
  33.  */
  34.  
  35. /* 
  36.  * Public Domain (p) by S. R. Sampson
  37.  * Version 2.3, October 1987
  38.  * Amiga port by Rick Schaeffer October 1987
  39.  *
  40.  * Rick Schaeffer          UUCP:  seismo!uunet!iscuva!ricks!ricks
  41.  * E. 13611 26th Ave.      Phone: (509)928-3533
  42.  * Spokane, WA  99216
  43.  *
  44.  * Modified path for CronTab & CronErr to suit Amiga enviroment better
  45.  * Version 2.31  <CB> 15.12.87 
  46.  *
  47.  * Fixed bug with CronTab entries specifying an event by date (month)
  48.  * i.e. "* * 24 12 * say Merry christmas", now works!
  49.  * Version 2.32 <CB> 25.12.87
  50.  *
  51.  * Removed "CronErr", an obvious Unix "feature"
  52.  * Version 2.33 <CB> 31.12.87 
  53.  *
  54.  * Additional support for Lattice 4.0, changed to complete sleep (no more  
  55.  * 5 I/O ints. per second), added command line parameter for CronTab path.
  56.  * Added feature to align Cron to start on 01 seconds, including "loss"
  57.  * due to lengthy command calls or extensive I/O.
  58.  * Version 2.4 <CB> 10.01.88 
  59.  *
  60.                           
  61.                           
  62. ______  /          
  63. ______\O                    - The Software Brewery - 
  64.       \\                          
  65.        o            Sparkling, fresh software from W.-Germany
  66.                  
  67.      @@@@@          Straight from the bar to your Amiga
  68.      |~~~|\        
  69.      | | |/        
  70.      |___|        With our regards to the Software Distillery
  71.  
  72. Members are (listed alphabetically):
  73. Christian Balzer alias <CB>, Lattice C, user interfaces, beer addict. 
  74. Christof Bonnkirch, Aztec C, telecommunications, money adict.
  75. Heiko Rath alias <HR>, Assembler, ROM-Kernal stuff, Marabou addict. 
  76. Peter Stark alias PS, Lattice C, IO & utilities, WordStar addict.
  77. Ralf Woitinas alias RAF, Assembler, anything, Ray-Tracing addict.
  78. Torsten Wronski alias MM, Assembler, anything, girls addict.
  79.  
  80. Beverages: Altenmuenster Brauer Bier, Urfraenkisches Landbier, Grohe Bock.
  81.  
  82. Send exotic drinks, comments, critizism, flames to:
  83.  
  84. The Software Brewery
  85. Christian Balzer
  86. Im Wingertsberg 45
  87. D-6108 Weiterstadt
  88. West-Germany
  89.  
  90. Our BBS "AmigaNode" isn't online yet. As soon as it becomes available, 
  91. you'll be the first to know :-).
  92.  
  93.  *
  94.  * Compilation notes:
  95.  * Just use the supplied "makefile.manx" for Aztec C 3.4(b) or later. 
  96.  * Lattice C 4.0 users just execute "lmake" or use "makefile.lattice".
  97.  * The whole compilation and link procedure (including timer.c) from RAM
  98.  * took me 50 seconds with Lattice 4.0 and 67 seconds with Aztec 3.4b.
  99.  * Please note that you need to link timer.o for the TimeDelay() function
  100.  * if you don't use make or execute lmake.
  101.  *
  102.  * Well, the Lattice folks claim that LATTICE get's defined by their 
  103.  * compiler, but it didn't work for me. So, if you don't use make or lmake, 
  104.  * or define LATTICE, you'll get blasted by (my) Lattice if ya don't remove 
  105.  * all the lines marked with "LK" below...
  106.  *                        <CB>
  107.  */
  108.  
  109. #include <stdio.h>
  110. #include <time.h>
  111.  
  112. #ifndef LATTICE            /*LK*/
  113. #include <functions.h>        /*LK*/
  114. #endif                /*LK*/
  115.  
  116. #ifdef LATTICE            /*LK*/
  117. #include <string.h>
  118. #include <proto/dos.h>
  119. #endif                /*LK -the last one*/
  120.  
  121. #define TRUE 1
  122. #define FALSE 0
  123.  
  124. #define CRONTAB "S:CronTab"    /* Our default path */
  125.  
  126. #define MAXLINE    132
  127. #define SIZE    64
  128.  
  129. #define TITLE "\x9B1;33mAmiCron v2.4\x9B0m by \x9B1m<CB>\x9B0m & - \x9B3mThe Software Brewery\x9B0m -\n"
  130. #define WHERE "Im Wingertsberg 45, D-6108 Weiterstadt, \x9B1mW-Germany\x9B0m\n"
  131.  
  132. int    eof;
  133. short    sleepy;
  134. char    min[SIZE], hour[SIZE], day[SIZE],
  135.     month[SIZE], wday[SIZE], command[SIZE];
  136. char    *tokv[] = { min, hour, day, month, wday };
  137. char    crontabs[200];  /* You might define quite a path! <CB> */
  138. FILE    *fd;
  139.  
  140.  
  141. /*
  142.  *    This is the basics, ready for bells and whistles
  143.  *
  144.  */
  145.  
  146.  
  147. void main(argc, argv)
  148. int argc;
  149. char *argv[];
  150. {
  151.     void showhelp();
  152.     void wakeup();
  153.  
  154.     /* Below we use the same functions & variables as in wakeup()
  155.        just to get the current second  <CB>*/
  156.  
  157.     register struct tm *tm;
  158.     struct tm *localtime();
  159.     long cur_time;
  160.      
  161.     /* Tell the guy out there what he might do with this deamon
  162.      * and who made it. <CB>*/
  163.  
  164.     printf(TITLE);
  165.     printf(WHERE);
  166.     printf("Usage: %s [Path & Filename]\n",argv[0]);
  167.     printf("       or '%s ?' for help\n",argv[0]);
  168.  
  169.     /* Now let's see if the user told us to look someplace else
  170.      * for our input file or if he want's some more usage hints <CB> */
  171.   
  172.     if (argc == 2) {
  173.         if (argv[1][0] == '?') { 
  174.             showhelp();
  175.             exit(10);
  176.         }
  177.         else (void)strcpy(crontabs, argv[1]);
  178.         
  179.     }
  180.     else (void)strcpy(crontabs, CRONTAB);    
  181.     /* If there isn't a user path, we'll use the default <CB>*/
  182.  
  183.     /*Now tell the user with what path & filename we wound up*/
  184.     printf("CronTab path = %s \n",crontabs);
  185.  
  186.     for (;;)  {
  187.  
  188.         wakeup();
  189.  
  190.         time(&cur_time);        /* get the current time */
  191.         tm = localtime(&cur_time);    /* break it down */
  192.  
  193.         /* Now we calculate the amount of seconds
  194.          * 'till the next minute <CB>*/
  195.  
  196.         sleepy = (60-tm->tm_sec);    
  197.         /* PLEASE don't decrease the 60 above, believe me,
  198.          * I know what I'm doing there! <CB> */ 
  199.  
  200.         TimeDelay(sleepy,0,0);    
  201.         /* sleep 'till next minute */
  202.     }
  203. }
  204.  
  205. void wakeup()
  206. {
  207.     register struct tm *tm;
  208.     struct tm *localtime();
  209.     long cur_time;
  210.     
  211.     char doit[80];
  212.      
  213.     time(&cur_time);        /* get the current time */
  214.     tm = localtime(&cur_time);    /* break it down */
  215.  
  216.     /* Now let's see if there is a CronTab file out there <CB> */     
  217.  
  218.     if ((fd = fopen(crontabs, "r")) == NULL) {
  219.     fprintf(stderr, "Can't open %s\nTry again\n", crontabs);
  220.     exit(1);
  221.     }
  222.  
  223.  
  224.     eof = FALSE;
  225.  
  226.     while (!eof)  {
  227.         if (getline() && match(min,tm->tm_min) &&
  228.            match(hour,tm->tm_hour) && match(day,tm->tm_mday) &&
  229.            match(month,tm->tm_mon+1) && match(wday,tm->tm_wday))  {
  230.         /* Weird localtime months ^ range from 0-11 !!! <CB>*/
  231.             (void)strcpy(doit,"RUN >nil: ");
  232.             (void)strcat(doit,command);
  233.             (void)Execute(doit,NULL,NULL);
  234.             }
  235.         }
  236.     fclose(fd);
  237. }
  238.  
  239.  
  240. /*
  241.  *    A line consists of six fields.  The first five are:
  242.  *
  243.  *        minute:         0-59
  244.  *        hour:           0-23
  245.  *        day:            1-31
  246.  *        month:          1-12
  247.  *        weekday:        0-6 (Sunday = 0)
  248.  *
  249.  *    The fields are seperated by spaces or tabs, with the
  250.  *    first field left justified (no leading spaces or tabs).
  251.  *    See below for optional field syntax.
  252.  *
  253.  *    The last field is the command field.  This command will
  254.  *    be executed by the CLI just as if typed from a console.
  255.  */
  256.  
  257. int getline()
  258. {
  259.     register char *p;
  260.     register int   i;
  261.     char    buffer[MAXLINE], *scanner(), *fgets();
  262.  
  263.     if (fgets(buffer, sizeof buffer, fd) == NULL)  {
  264.         eof = TRUE;
  265.         return(FALSE);
  266.         }
  267.  
  268.     for (p = buffer, i = 0; i < 5; i++)  {
  269.         if ((p = scanner(tokv[i], p)) == (char *)NULL)
  270.             return(FALSE);
  271.         }
  272.  
  273.     (void)strcpy(command, p);     /* scoop the command */
  274.     return(TRUE);
  275. }
  276.  
  277.  
  278. char *scanner(token, offset)
  279. register char   *token;        /* target buffer to receive scanned token */
  280. register char   *offset;    /* place holder into source buffer */
  281. {
  282.     while ((*offset != ' ') && (*offset != '\t') && *offset)
  283.         *token++ = *offset++;
  284.  
  285.     /*
  286.      *      Check for possible error condition
  287.      */
  288.          
  289.     if (!*offset)
  290.         return ((char *)NULL);
  291.  
  292.     *token = '\0';
  293.         
  294.     while ((*offset == ' ') || (*offset == '\t'))
  295.         offset++;
  296.  
  297.     return (offset);
  298. }
  299.  
  300.  
  301. /*
  302.  *    This routine will match the left string with the right number.
  303.  *
  304.  *    The string can contain the following syntax:
  305.  *
  306.  *    *        This will return TRUE for any number
  307.  *    x,y [,z, ...]    This will return TRUE for any number given.
  308.  *    x-y        This will return TRUE for any number within
  309.  *            the range of x thru y.
  310.  */
  311.  
  312. int match(left, right)
  313. register char   *left;
  314. register int    right;
  315. {
  316.     register int    n;
  317.     register char    c;
  318.  
  319.     n = 0;
  320.  
  321.     if (!strcmp(left, "*"))
  322.         return(TRUE);
  323.  
  324.     while ((c = *left++) && (c >= '0') && (c <= '9'))
  325.         n  =  (n * 10) + c - '0';
  326.  
  327.     switch (c)  {
  328.         case '\0':
  329.             return (right == n);
  330.  
  331.         case ',':
  332.             if (right == n)
  333.                 return(TRUE);
  334.             do {
  335.                 n = 0;
  336.                 while ((c = *left++) && (c >= '0') && (c <= '9'))
  337.                     n = (n * 10) + c - '0';
  338.                  if (right == n)
  339.                     return(TRUE);
  340.                 } while (c == ',');
  341.             return(FALSE);
  342.  
  343.         case '-':
  344.             if (right < n)
  345.                 return(FALSE);
  346.  
  347.             n = 0;
  348.             while ((c = *left++) && (c >= '0') && (c <= '9'))
  349.                 n = (n * 10) + c - '0';
  350.  
  351.             return(right <= n);
  352.         }
  353. }
  354.  
  355. void showhelp()        /* by <CB> */
  356.  
  357.     printf("\nWell, you really should read the .doc file,\n");
  358.     printf("but here are some more hints:\n");
  359.     printf("You might specify only ONE command line parameter,\n");
  360.     printf("the path were Amicron should look for it's command table,\n");
  361.     printf("including the filename of the command table.\n");
  362.     printf("If you don't supply a path, Amicron will use it's default\n");
  363.     printf("path (S:CronTab).\n");
  364. }
  365.